package com.sjm.suchbear.minibookmall.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.OrderActivity;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.CarManage;
import com.sjm.suchbear.minibookmall.fragment.adapter.CarListAdapter;
import com.sjm.suchbear.minibookmall.model.CarModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarFragment extends Fragment {


    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.lv_carList)
    MyListView lvCarList;
    @BindView(R.id.tv_allChoose)
    TextView tvAllChoose;
    @BindView(R.id.tv_button)
    TextView tvButton;
    @BindView(R.id.rl_button)
    RelativeLayout rlButton;
    Unbinder unbinder;

    private SQLiteDatabase dbread;
    private static List<CarModel> cars;
    private CarListAdapter carListAdapter;

    public CarFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_car, container, false);
        unbinder = ButterKnife.bind(this, view);
        llHeaderBack.setVisibility(View.GONE);
        tvListTitle.setText("购物车");
        BookDB db = new BookDB(getActivity());
        dbread = db.getReadableDatabase();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        cars = CarManage.getCarbyid(dbread, SP.getUid(getActivity()));
        if (cars != null) {
            initMode(0);
        } else {
            initMode(2);
        }
        carListAdapter = new CarListAdapter(getActivity(), cars);
        lvCarList.setAdapter(carListAdapter);
        lvCarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (carListAdapter.getisCheckMode()) {
                    cars.get(i).setChecked(!cars.get(i).isChecked());
                    carListAdapter.notifyDataSetChanged();
                }
            }
        });

        lvCarList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!carListAdapter.getisCheckMode()) {
                    carListAdapter.isCheckedMode(true);
                    initMode(1);
                    carListAdapter.notifyDataSetChanged();
                    cars.get(i).setChecked(true);
                }
                return false;
            }
        });
    }

    private void initMode(int mode) {
        switch (mode) {
            case 0:
                llHeaderBack.setVisibility(View.GONE);
                rlButton.setVisibility(View.VISIBLE);
                tvAllChoose.setVisibility(View.GONE);
                tvButton.setText("结算");
                break;
            case 1:
                llHeaderBack.setVisibility(View.VISIBLE);
                rlButton.setVisibility(View.VISIBLE);
                tvAllChoose.setVisibility(View.VISIBLE);
                tvButton.setText("删除");
                break;
            case 2:
                llHeaderBack.setVisibility(View.GONE);
                rlButton.setVisibility(View.GONE);
                break;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static void setChecked(int position, boolean checked) {
        cars.get(position).setChecked(checked);
    }

    @OnClick({R.id.tv_allChoose, R.id.tv_button, R.id.ll_header_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_allChoose:
                if (carListAdapter.getisCheckMode()) {
                    for (int i = 0; i < cars.size(); i++) {
                        cars.get(i).setChecked(true);
                    }
                    carListAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.tv_button:
                if (carListAdapter.getisCheckMode()) {
                    //删除

                    for (int i = 0; i < cars.size(); i++) {
                        if (cars.get(i).isChecked()) {
                            CarManage.deleteCarByBook(dbread, SP.getUid(getActivity()), cars.get(i).getBookID());
                        }
                    }
                    //退出删除模式
                    carListAdapter.isCheckedMode(false);
                    initMode(0);
                    cars.removeAll(cars);
                    List<CarModel> temp = CarManage.getCarbyid(dbread, SP.getUid(getActivity()));
                    if (temp != null) {
                        cars.addAll(temp);
                    }
                    carListAdapter.notifyDataSetChanged();
                } else {
                    //结算
                    Intent intent = new Intent(getActivity(), OrderActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.ll_header_back:
                if (carListAdapter.getisCheckMode()) {
                    carListAdapter.isCheckedMode(false);
                    for (int i = 0; i < cars.size(); i++) {
                        cars.get(i).setChecked(false);
                    }
                    carListAdapter.notifyDataSetChanged();
                    initMode(0);
                }
                break;
        }
    }


}
